/*
 * Copyright (C) 2012 Le Tuan Anh <anh.takaji@gmail.com>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package ezcms.dal.hibernate.mysql;

import ezcms.dal.ConferenceDAO;
import ezcms.dto.Conference;
import ezcms.dto.Location;
import ezcms.dto.Topic;
import ezcms.exceptions.DataLayerException;
import java.util.Date;
import java.util.List;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.Restrictions;

/**
 *
 * @author Le Tuan Anh <anh.takaji@gmail.com>
 */
class ConferenceDAOImpl implements ConferenceDAO {

    public ConferenceDAOImpl() {
    }

    @Override
    public List<Conference> getAllConferences() throws DataLayerException {
        return HibernateUtil.getListByConditions(Conference.class, null, "Cannot retrieve all conferences");
    }

    @Override
    public Conference getConferenceById(long id) throws DataLayerException {
        Object result = HibernateUtil.getObjectById(Conference.class, id, "Cannot find conference object with id = " + id);
        return (result == null) ? null : (Conference) result;
    }

    @Override
    public List<Conference> getActiveConferences() throws DataLayerException {
        Date currentDate = new Date();
        Criterion conditions = Restrictions.and(Restrictions.gt("endDate", currentDate), Restrictions.lt("startDate", currentDate));
        return HibernateUtil.getListByConditions(Conference.class, conditions, "Cannot retrieve active conferences");
    }

    @Override
    public List<Conference> getActiveConferencesByLocation(Location l) throws DataLayerException {
        Date currentDate = new Date();
        // Location == l && endDate < currentDate && startDate > currentDate
        Criterion conditions =
                Restrictions.and(
                Restrictions.and(Restrictions.gt("endDate", currentDate),
                Restrictions.lt("startDate", currentDate)),
                Restrictions.eq("location", l));
        return HibernateUtil.getListByConditions(Conference.class, conditions, "Cannot retrieve active conferences");
    }

    @Override
    public void saveConference(Conference conference) throws DataLayerException {
        System.out.println("Attempting to save in DAO");
        Conference current = getConferenceById(conference.getId());
        if (current == null) {
            HibernateUtil.saveObject(conference, "Cannot save conference");
        } else {
            System.out.println("Test save back immediately ..." + conference);
            HibernateUtil.saveObject(current, "Cannot update conference");
            System.out.println("It seems ok, now try to commit ..." + conference);
            
//            conference.updateTo(current);
//            HibernateUtil.saveObject(current, "Cannot update conference");
            HibernateUtil.saveObject(conference, "Cannot update conference");
        }
    }

    @Override
    public void deleteConference(Conference conference) throws DataLayerException {
        HibernateUtil.deleteObject(conference, "Cannot delete conference");
    }

    @Override
    public List<Location> getAllLocations() throws DataLayerException {
        return HibernateUtil.getListByConditions(Location.class, null, "Cannot retrieve all locations");
    }

    @Override
    public Location getLocationById(long id) throws DataLayerException {
        Object result = HibernateUtil.getObjectById(Location.class, id, "Cannot find location object with id = " + id);
        return (result == null) ? null : (Location) result;
    }

    @Override
    public Location getLocationByName(String name) throws DataLayerException {
        Object result = HibernateUtil.getObjectByConditions(Location.class, Restrictions.eq("name", name), "Cannot find location object with id = " + name);
        return (result == null) ? null : (Location) result;
    }

    @Override
    public void saveLocation(Location location) throws DataLayerException {
        HibernateUtil.saveObject(location, "Cannot save given location");
    }

    @Override
    public void deleteLocation(Location location) throws DataLayerException {
        HibernateUtil.deleteObject(location, "Cannot delete given location");
    }

    @Override
    public List<Topic> getAllTopics() throws DataLayerException {
        return HibernateUtil.getListByConditions(Topic.class, null, "Cannot retrieve all Topics");
    }

    @Override
    public Topic getTopicById(long id) throws DataLayerException {
        Object result = HibernateUtil.getObjectById(Topic.class, id, "Cannot find Topic object with id = " + id);
        return (result == null) ? null : (Topic) result;
    }

    @Override
    public void saveTopic(Topic topic) throws DataLayerException {
        HibernateUtil.saveObject(topic, "Cannot save Topic");
    }

    @Override
    public void deleteTopic(Topic topic) throws DataLayerException {
        HibernateUtil.deleteObject(topic, "Cannot delete Topic");
    }

    @Override
    public Topic getTopic(Conference conference, String title) throws DataLayerException {
        Criterion query = Restrictions.and(
                Restrictions.eq("conference", conference), Restrictions.eq("title", title));
        Object result = HibernateUtil.getObjectByConditions(Topic.class, query, "Cannot retrieve topic with title = " + title);
        return (result == null) ? null : (Topic) result;
    }

    @Override
    public Conference getConference(Location location, String title) throws DataLayerException {
        Criterion query = Restrictions.and(
                Restrictions.eq("location", location), Restrictions.eq("title", title));
        Object result = HibernateUtil.getObjectByConditions(Conference.class, query, "Cannot retrieve conference with title = " + title);
        return (result == null) ? null : (Conference) result;
    }
}
